######################################################
# REPLICATION FILE FOR
# "Sustaining Capitalism and Democracy: Lessons from 
#     Global Competition Policy"
# By Michael O Allen and Kenneth Scheve
#
# IN THIS FILE:
#   Construct event history data set
######################################################

library(data.table)

setwd('/Users/moda/Desktop/ISR Replication/Data')

# Functions ---------------------------------------------------------------

create_gt <- function (x) {
  # Find rows of events, append 1 for beginning row to start counter
  eventHolder <- c(0, which(x == 1))
  
  # Check if last observation is an event, if not then add length for closing deq
  if (max(eventHolder) != length(x)) {
    eventHolder <- append(eventHolder,
                          length(x))
  }
  
  # Create vector of duration between legislation
  breaks <- as.integer()
  
  for (i in 1:(length(eventHolder)-1)) {
    breaks <- append(breaks,
                     (eventHolder[i+1] - eventHolder[i]))
  }
  
  gap_time <- unlist(sapply(breaks, function (y) seq(1, y, 1)))
  return(gap_time)
}


# Import CCL --------------------------------------------------------------

cli <- setDT(haven::read_dta('/Users/moda/Dropbox/Work/Yale/AntiTrust/Data/CCL_Original/CCL_CLI_Data_Ver2.dta'))

fullData <- cli[regional_group == 0, .(country, code_cown, code_cowc, year,
                                       competition_law_coded, cli_overall_norm)]

rm(cli)

# Create gap time variable ------------------------------------------------
# Gap-time on leg
fullData[, leg_gt := create_gt(competition_law_coded), by = country]
fullData[, leg_gt0 := leg_gt - 1]

# leg counter
fullData[ , leg_counter := cumsum(competition_law_coded), by = country]
fullData[ , leg_counter := shift(leg_counter, 1, 0, 'lag'), by = country]

rm(create_gt)

# Merge with Polity5 ------------------------------------------------------

polity <- setDT(readxl::read_xls('/Users/moda/Dropbox/Data/Governance/Polity5/p5v2018.xls'))

fullData <- merge(fullData, polity[ , .(code_cowc = scode, year, polity2)],
                  by = c('code_cowc', 'year'),
                  all.x = T)

fullData$dem <- ifelse(fullData$polity2 > 6, 'Democracy', 'Non-democracy')

setorder(fullData, country, year)

rm(polity)

# Merge Maddison ----------------------------------------------------------

madd <- readxl::read_xlsx('/Users/moda/Dropbox/Work/Yale/AntiTrust/Data/Income/mpd2020.xlsx',
                          sheet = 'Full data')

madd$code_cowc <- countrycode::countrycode(madd$countrycode, 'iso3c', 'cowc')
madd <- madd[!is.na(madd$code_cowc), ]

fullData <- merge(fullData, madd[, c("code_cowc", "year", "gdppc", "pop")],
                  by = c("code_cowc", "year"),
                  all.x = TRUE)

rm(madd)

# Trade Openness ----------------------------------------------------------
# WDI data used in paper was downloaded in July 2021.

indicators <- c(trade_open = "NE.TRD.GNFS.ZS")

wdi <- WDI::WDI(indicator = indicators, extra = T)
wdi$code_cowc <- countrycode::countrycode(wdi$iso3c, 'iso3c', 'cowc')
wdi <- wdi[!is.na(wdi$code_cowc), ]

fullData <- merge(fullData, wdi[ , c("code_cowc", "year", "region", "trade_open")],
                  by = c("code_cowc", "year"),
                  all.x = TRUE)

rm(wdi)

# Merge with inequality ---------------------------------------------------

wid <- fread('/Users/moda/Dropbox/Work/Yale/AntiTrust/Data/Income/WID_p99p100.csv',
             na.strings = '')
wid$code_cowc <- countrycode::countrycode(wid$country, 'iso2c', 'cowc')
# Add KV, RS, PS
wid[country == "KV", code_cowc := "KOS"]

wid <- wid[!(country == 'FR' & pop != 'j'), ]
colnames(wid)[5] <- 'top1Share'

setorder(wid, country, year)

wid[, top1Share_percentdiff := (top1Share / shift(top1Share, 1, fill = NA)) - 1, 
    by = country]

fullData <- merge(fullData, wid[ , .(code_cowc, year, 
                                     top1Share,
                                     top1Share_percentdiff)],
                  by = c("code_cowc", "year"),
                  all.x = TRUE)

rm(wid)


# Crises ------------------------------------------------------------------

crisis <- setDT(readxl::read_xlsx('/Users/moda/Downloads/20160923_global_crisis_data.xlsx'))

crisis$cc <- ifelse(crisis$`Currency Crises` > 0, 1, 0)
crisis$`Banking Crisis`[crisis$`Banking Crisis` == 'n/a'] <- NA
crisis$bc <- ifelse(crisis$`Banking Crisis` > 0, 1, 0)
crisis$ic <- ifelse(crisis$`Inflation Crises` > 0, 1, 0)

crisis$code_cowc <- countrycode::countrycode(crisis$CC3,
                                             'iso3c',
                                             'cowc')
crisis[ , fin_crisis := ic + bc + cc]
crisis$fin_crisis_bin <- ifelse(crisis$fin_crisis > 0, 1, 0)

crisis[, fin_crisis_rollmean_5 := frollmean(fin_crisis_bin, 5, fill = NA, align = 'right'),
       by = Country]
crisis[, fin_crisis_rollmean_3 := frollmean(fin_crisis_bin, 3, fill = NA, align = 'right'),
       by = Country]

fullData <- merge(fullData, crisis[ , .(code_cowc, year = Year, 
                                        fin_crisis_rollmean_3, fin_crisis_rollmean_5)],
                  by = c('code_cowc', 'year'),
                  all.x = TRUE)

# Transformations ---------------------------------------------------------

fullData[ , pop_ln := log(pop*1000)]
fullData[ , gdppc_ln := log(gdppc)]
fullData[ , trade_open_ln := log(trade_open)]

fullData$dem_bin <- ifelse(fullData$polity2 > 6, 1, 0)

# Write -------------------------------------------------------------------

fwrite(fullData, 'event_history.csv')
